import fetch from "node-fetch";
import * as cheerio from "cheerio";
import type { MirrorWebsiteFile } from "../common/spider-types";

async function main() {
  // const url = "https://mirrors.aliyun.com/apache/maven/maven-3/";

  // const url = "https://mirrors.cloud.tencent.com/apache/zookeeper/";
  // const url = "https://mirrors.huaweicloud.com/apache/maven/mvnd/";
  // const url = "https://mirrors.huaweicloud.com/apache/maven/mvnd/2.0.0-beta-1/";

  // const url = "https://dlcdn.apache.org/zookeeper/";
  const url = "https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/";

  const resp = await fetch(url);
  const html = await resp.text();

  const $ = cheerio.load(html);
  let fileList: MirrorWebsiteFile[];
  if (url.includes("mirrors.aliyun.com")) {
    // 阿里云镜像
    fileList = parseAliyun($);
  } else if (url.includes("mirrors.huaweicloud.com") || url.includes("mirrors.cloud.tencent.com")) {
    // 华为云和腾讯云镜像
    fileList = parse1($);
  } else {
    // Apache和清华大学
    fileList = parse0($);
  }
  for (const it of fileList) {
    console.log(it);
  }
}

function parseAliyun($: cheerio.CheerioAPI): MirrorWebsiteFile[] {
  const table = $(".mirror-content .mirror-left .table");
  const tableRows = table.find("tbody tr");

  const fileList: MirrorWebsiteFile[] = [];
  tableRows.each((_, tr) => {
    const $this = $(tr);
    const item = $this.find(".link a").text().trim();
    if (!item || item === "../" || item.toLowerCase().startsWith("parent directory")) return;

    const type = item.endsWith("/") ? "dir" : "file";
    const name = item.replace(/\/$/, "");

    const size = $this.find(".size").text().trim();
    const date = $this.find(".date").text().trim();
    fileList.push({ name, type, date, size: size === "-" ? undefined : size });
  });

  return fileList;
}

function parse0($: cheerio.CheerioAPI): MirrorWebsiteFile[] {
  // Apache和清华镜像有一条水平线隔开文件列表
  const fileList: MirrorWebsiteFile[] = [];

  const content = $("pre").text();
  // console.log(content);
  const lines = content.split(/[\r\n]/).slice(1);
  for (const it of lines) {
    const line = it.trim();
    if (!line || line.includes("Parent Directory")) {
      continue;
    }

    fileList.push(parseFileInfo(line));
  }

  return fileList;
}

function parse1($: cheerio.CheerioAPI): MirrorWebsiteFile[] {
  const content = $("pre").last().text();
  const fileList: MirrorWebsiteFile[] = [];
  const lines = content.split(/[\r\n]/);
  for (const it of lines) {
    const line = it.trim();
    if (!line || line === "../") continue;

    fileList.push(parseFileInfo(line));
  }

  return fileList;
}

function parseFileInfo(line: string): MirrorWebsiteFile {
  const [name, date0, time0, ...sizeTokens] = line.split(/\s+/);
  const date = new Date(date0 + " " + time0);
  let size = sizeTokens.join("");
  if (size === "-") {
    size = undefined;
  }
  return {
    name: name.replace(/\/$/, ""),
    type: name.endsWith("/") ? "dir" : "file",
    date: date.toLocaleString(),
    size,
  };
}

main();
